package com.kunyi.web.service.file.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kunyi.platform.tool.exception.BusinessException;
import com.kunyi.web.service.file.core.enums.ExportTemplateStatusEnum;
import com.kunyi.web.service.file.core.transform.ExportTemplateTransform;
import com.kunyi.web.service.file.entity.domain.ExportTemplate;
import com.kunyi.web.service.file.entity.req.TemplateSaveReq;
import com.kunyi.web.service.file.mapper.IExportTemplateMapper;
import com.kunyi.web.service.file.service.IExportTemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName ExportTemplateServiceImpl
 * @Description export_template表相关业务实现
 * @Author pengjunjun
 * @Date 2024/03/04
 * @Version 1.0
 **/
@Service
public class ExportTemplateServiceImpl extends ServiceImpl<IExportTemplateMapper, ExportTemplate> implements IExportTemplateService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long save(TemplateSaveReq req) {
        boolean isExist = baseMapper.isExistByCode(req.getCode());
        if(isExist){
            throw new BusinessException("模板code已存在！");
        }
        // req -> domain
        ExportTemplate template = ExportTemplateTransform.saveReq2Template(req)
                .setStatus(ExportTemplateStatusEnum.DRAFT.getCode());
        boolean save = save(template);
        if(!save){
            throw new BusinessException("保存模板失败！");
        }
        return template.getId();
    }

    @Override
    public ExportTemplate getByCode(String code) {
        return baseMapper.getByCode(code);
    }
}
